package org.mms.controller;

import org.mms.pojo.dto.MaterialInboundDto;
import org.mms.pojo.entity.MaterialInbound;
import org.mms.pojo.request.MaterialInboundRequest;
import org.mms.pojo.request.MaterialInboundReviewRequest;
import org.mms.pojo.util.ResponseMessage;
import org.mms.service.InboundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/material-inbounds")
public class InboundController {
    @Autowired
    private InboundService inboundService;

    @GetMapping
    public ResponseMessage<List<MaterialInboundDto>> list() {
        return ResponseMessage.success(inboundService.list()
                .stream()
                .map(MaterialInbound::convert)
                .toList());
    }

    @GetMapping("/{id}")
    public ResponseMessage<MaterialInboundDto> getById(@PathVariable Long id) {
        return ResponseMessage.success(inboundService.getById(id)
                .convert());
    }

    @PostMapping
    public ResponseMessage<MaterialInboundDto> create(@RequestBody MaterialInboundRequest request) {
        return ResponseMessage.success(inboundService.create(request)
                .convert());
    }

    @PutMapping
    public ResponseMessage<MaterialInboundDto> update(@RequestBody MaterialInboundRequest request) {
        return ResponseMessage.success(inboundService.update(request)
                .convert());
    }

    @PutMapping("/review")
    public ResponseMessage<MaterialInboundDto> review(@RequestBody MaterialInboundReviewRequest request) {
        return ResponseMessage.success(inboundService.review(request)
                .convert());
    }

    @DeleteMapping("/{id}")
    public ResponseMessage<MaterialInboundDto> delete(@PathVariable Long id) {
        inboundService.delete(id);
        return ResponseMessage.success(null);
    }
}